<?php

namespace Task\Controller;

use Common\Controller\BaseController;
use Think\Page;

class IndexController extends BaseController
{
    public function index()
    {
        $get_task_type = $_GET['task_type'];
        $model = D('VUserTask');
        $sp = $_GET['p'];
        if (empty($sp)) {
            $sp = 1;
        }
        $search_key = $_POST['search_key'];
        $status = $_POST['search_status'];

        $task_type = $_POST['task_type'];

        if (is_numeric($get_task_type)) {
            $task_type = $get_task_type;
        }

        $this->assign('task_type', $task_type);

        $user_id = $this->current_user['id'];
        $list = $model->get_list($user_id, $search_key, $status, $task_type, null, 0, 0, $sp, C('PAGE_SIZE'));

        $page = new Page($list[1], C('PAGE_SIZE'));

        $this->assign('total_rows', $page->totalRows);
        $this->assign('_page', $page);
        $this->assign('page_link', $page->show());
        $this->assign('task_list', $list[0]);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }
        $this->display();
    }

    public function edit()
    {
        $id = $_GET['id'];
        $usertaskmodel = D('VUserTask');
        $user_task = $usertaskmodel->get($id);

        $this->assign('user_task', $user_task);

        $usertaskannexmodel = D('UserTaskAnnex');
        $condition = array();
        $condition['user_task_id'] = array('eq', $id);
        $annex_list = $usertaskannexmodel->where($condition)->order('id asc')->select();
        $this->assign('annex_list', $annex_list);
        $this->display();
    }

    public function save()
    {
        $task_type = $_POST['task_type'];
        $model = D('UserTask');
        $data = $model->create();
        if ($data['status'] == 2) {
            $data['is_back'] = 0;
            $data['report_at'] = time();
        }
        $result = $model->save($data);

        if (!$result && $result != '') {
            echo $model->getError();
        } else {
            $annex_name = $_POST['annex_name'];
            $annex_path = $_POST['annex_path'];

            $cmodel = D('UserTaskAnnex');
            for ($i = 0; $i < count($annex_name); $i++) {
                if (!empty($annex_name[$i])) {
                    $cdata = $cmodel->create();
                    $name = $annex_name[$i];
                    $ename = end(explode(".", $name));
                    $atype = 0;
                    if (strtolower($ename) == 'doc') {
                        $atype = 1;
                    } else {
                        $atype = 2;
                    }
                    $cdata['id'] = null;
                    $cdata['annex_name'] = $name;
                    $cdata['annex_path'] = $annex_path[$i];
                    $cdata['annex_type'] = $atype;
                    $cdata['extension'] = $ename;
                    $cdata['user_task_id'] = $data['id'];
                    $cmodel->add($cdata);
                }
            }
        }

        redirect('/task/index/index?task_type=' . $task_type);
    }

    public function deleteannex()
    {
        $id = $_POST['id'];

        $usertaskannexmodel = D('UserTaskAnnex');

        $result = $usertaskannexmodel->delete($id);
        if (!$result && $result != '') {
            echo $usertaskannexmodel->getError();
        } else {
            echo 0;
        }
    }
}
